<?php
/* 
 * FST Extension: Photojournal
 * Abe Yang <abeyang@cal.berkeley.edu> (c) 2007
 * http://code.google.com/p/flickrsetsbytag/
 *
/* ---------------------------------------------------------------------------------- */

require('fst.class.php');

class PhotojournalEntry extends FST {
	var $album;
	var $primary;
	
	function Photojournal($cache_dir = FST_CACHE_DIR) {
		parent :: FST($cache_dir);
	} // end Photojournal()

	// $albumid: id for flickr photo set
	// $matchtag: matching tag
	
	function setAlbum($albumid, $matchtag = FST_DEFAULT_TAG) {
		$matchtag = checkTag($matchtag);
		$this->album = $this->getAlbum($albumid, $matchtag);
		$this->primary = $this->getPrimary();
		
		// print_r($this->album['photo']);
	}	// end setAlbum()
	
	function getPrimary() {
		// NOTE: this is expensive; must be done to get primary's description
		foreach($this->album['photo'] as $photo) {
			if ($photo['isprimary']) return $photo;
		}
		// cannot find primary: use first photo
		return $this->album['photo'][0];
	}	// end getPrimary()	
	
	function getPath($photo) {
		return $photo['server'] . '/' . $photo['id'] . '_' . $photo['secret'];
	}	// end getPath()
	
	// $i = index of photo array
	function stringifyPhoto($photo, $i) {
		$unique_id = $this->getPath($photo);
		$caption = str_replace("\n", '', $photo['description']);
		return $i.':'.'{"id":"'.$unique_id.'", "caption":"'.$caption.'"}, '."\n";
	}	// end stringifyPhoto()
	
	function jsonifyPhotos() {
		// init $json with primary photo
		$json = $this->stringifyPhoto($this->getPrimary(), 0);
		
		$i = 1;
		foreach($this->album['photo'] as $photo) {
			// if primary is already used as first image, we skip it
			if ($photo['isprimary']) {
				// $i++;
				continue;
			}
			$json .= $this->stringifyPhoto($photo, $i);
			$i++;
		}
		return $json;
	}	// end jsonifyPhotos()
	
	function getSize() {
		return sizeof($this->album['photo']);
	}	// end getSize()	

}	// end class
